library(plyr)
library(dplyr)
library(tidyverse)
library(gsynth)
library(stargazer)
library(modelsummary) # https://modelsummary.com/articles/modelsummary.html#new-models-and-custom-statistics

setwd("~/Dropbox/China Huawei Paper/Production Materials/Replication/")
master.data <- read.csv("dataFile_China_Huawei.csv"); head(master.data)

fh = read.csv('FOTN 2011-2023 Score Data.csv')
fh = fh[c("Country","Edition","A..Obstacles.to.Access","B..Limits.on.Content","C..Violations.of.User.Rights","Total.country.score")]
names(fh) = c("country_name","year","Obstacles.to.Access","Limits.on.Content","Violations.of.User.Rights","Total.country.score")
master.data = join(master.data,fh,by=c("country_name","year"))

master.data = master.data[master.data$cabb!="MNG",]
master.data = master.data[master.data$cabb!="SSD",]


# 250k
dat <- master.data
dat$treated = 0
threshold=250000
dat = as.data.frame(dat %>%
                      group_by(cabb) %>%
                      dplyr::mutate(treated = ifelse(row_number() == 1, 0, NA),
                                    treated = ifelse(china.huawei > threshold, 1, treated)) %>%
                      fill(treated))
dat$treated[is.na(dat$treated)==T] <- 0

sub = dat[dat$v2x_polyarchy<=0.42,]
sub = sub[is.na(sub$v2smgovfilprc)==F & is.na(sub$treated)==F & is.na(sub$v2x_polyarchy)==F & is.na(sub$pres.election)==F & is.na(sub$coup.attempts)==F & is.na(sub$successful.coups)==F,]
sub = sub[is.na(sub$pcGDPcur)==F & is.na(sub$GDPcur)==F & is.na(sub$electricitypc)==F,]
sub = sub[is.na(sub$Obstacles.to.Access)==F & is.na(sub$Limits.on.Content)==F & is.na(sub$Violations.of.User.Rights)==F,]
nrow(dat);nrow(sub)

# all use min.T0=3
out1 <- gsynth(Obstacles.to.Access ~ treated + v2x_polyarchy + pres.election + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=3, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
out1$est.avg
out1est250 = out1$est.avg

out2 <- gsynth(Limits.on.Content ~ treated + v2x_polyarchy + pres.election + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=3, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
out2$est.avg
out2est250 = out2$est.avg

out3 <- gsynth(Violations.of.User.Rights ~ treated + v2x_polyarchy + pres.election + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=3, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
out3$est.avg
out3est250 = out3$est.avg

out4 <- gsynth(Total.country.score ~ treated + v2x_polyarchy + pres.election + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=3, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
out4$est.avg
out4est250 = out4$est.avg


# 500k
dat <- master.data
dat$treated = 0
threshold=500000 #, good
dat = as.data.frame(dat %>%
                      group_by(cabb) %>%
                      dplyr::mutate(treated = ifelse(row_number() == 1, 0, NA),
                                    treated = ifelse(china.huawei > threshold, 1, treated)) %>%
                      fill(treated))
dat$treated[is.na(dat$treated)==T] <- 0

sub = dat[dat$v2x_polyarchy<=0.42,]
sub = sub[is.na(sub$v2smgovfilprc)==F & is.na(sub$treated)==F & is.na(sub$v2x_polyarchy)==F & is.na(sub$pres.election)==F & is.na(sub$coup.attempts)==F & is.na(sub$successful.coups)==F,]
sub = sub[is.na(sub$pcGDPcur)==F & is.na(sub$GDPcur)==F & is.na(sub$electricitypc)==F,]
sub = sub[is.na(sub$Obstacles.to.Access)==F & is.na(sub$Limits.on.Content)==F & is.na(sub$Violations.of.User.Rights)==F,]
nrow(dat);nrow(sub)

# all use min.T0=3
out1 <- gsynth(Obstacles.to.Access ~ treated + v2x_polyarchy + pres.election + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=3, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
out1$est.avg
out1est500 = out1$est.avg

out2 <- gsynth(Limits.on.Content ~ treated + v2x_polyarchy + pres.election + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=3, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
out2$est.avg
out2est500 = out2$est.avg

out3 <- gsynth(Violations.of.User.Rights ~ treated + v2x_polyarchy + pres.election + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=3, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
out3$est.avg
out3est500 = out3$est.avg

out4 <- gsynth(Total.country.score ~ treated + v2x_polyarchy + pres.election + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=3, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
out4$est.avg
out4est500 = out4$est.avg


# 1 million
dat <- master.data
dat$treated = 0
threshold=1000000
dat = as.data.frame(dat %>%
                      group_by(cabb) %>%
                      dplyr::mutate(treated = ifelse(row_number() == 1, 0, NA),
                                    treated = ifelse(china.huawei > threshold, 1, treated)) %>%
                      fill(treated))
dat$treated[is.na(dat$treated)==T] <- 0

sub = dat[dat$v2x_polyarchy<=0.42,]
sub = sub[is.na(sub$v2smgovfilprc)==F & is.na(sub$treated)==F & is.na(sub$v2x_polyarchy)==F & is.na(sub$pres.election)==F & is.na(sub$coup.attempts)==F & is.na(sub$successful.coups)==F,]
sub = sub[is.na(sub$pcGDPcur)==F & is.na(sub$GDPcur)==F & is.na(sub$electricitypc)==F,]
sub = sub[is.na(sub$Obstacles.to.Access)==F & is.na(sub$Limits.on.Content)==F & is.na(sub$Violations.of.User.Rights)==F,]
nrow(dat);nrow(sub)

# all use min.T0=3
out1 <- gsynth(Obstacles.to.Access ~ treated + v2x_polyarchy + pres.election + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=3, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
out1$est.avg
out1est1m = out1$est.avg

out2 <- gsynth(Limits.on.Content ~ treated + v2x_polyarchy + pres.election + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=3, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
out2$est.avg
out2est1m = out2$est.avg

out3 <- gsynth(Violations.of.User.Rights ~ treated + v2x_polyarchy + pres.election + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=3, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
out3$est.avg
out3est1m = out3$est.avg

out4 <- gsynth(Total.country.score ~ treated + v2x_polyarchy + pres.election + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=3, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
out4$est.avg
out4est1m = out4$est.avg


# 5 million
dat <- master.data
dat$treated = 0
threshold=5000000
dat = as.data.frame(dat %>%
                      group_by(cabb) %>%
                      dplyr::mutate(treated = ifelse(row_number() == 1, 0, NA),
                                    treated = ifelse(china.huawei > threshold, 1, treated)) %>%
                      fill(treated))
dat$treated[is.na(dat$treated)==T] <- 0

sub = dat[dat$v2x_polyarchy<=0.42,]
sub = sub[is.na(sub$v2smgovfilprc)==F & is.na(sub$treated)==F & is.na(sub$v2x_polyarchy)==F & is.na(sub$pres.election)==F & is.na(sub$coup.attempts)==F & is.na(sub$successful.coups)==F,]
sub = sub[is.na(sub$pcGDPcur)==F & is.na(sub$GDPcur)==F & is.na(sub$electricitypc)==F,]
sub = sub[is.na(sub$Obstacles.to.Access)==F & is.na(sub$Limits.on.Content)==F & is.na(sub$Violations.of.User.Rights)==F,]
nrow(dat);nrow(sub)

# all use min.T0=3
out1 <- gsynth(Obstacles.to.Access ~ treated + v2x_polyarchy + pres.election + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=3, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
out1$est.avg
out1est5m = out1$est.avg

out2 <- gsynth(Limits.on.Content ~ treated + v2x_polyarchy + pres.election + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=3, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
out2$est.avg
out2est5m = out2$est.avg

out3 <- gsynth(Violations.of.User.Rights ~ treated + v2x_polyarchy + pres.election + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=3, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
out3$est.avg
out3est5m = out3$est.avg

out4 <- gsynth(Total.country.score ~ treated + v2x_polyarchy + pres.election + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=3, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
out4$est.avg
out4est5m = out4$est.avg


# 10 million
dat <- master.data
dat$treated = 0
threshold=10000000
dat = as.data.frame(dat %>%
                      group_by(cabb) %>%
                      dplyr::mutate(treated = ifelse(row_number() == 1, 0, NA),
                                    treated = ifelse(china.huawei > threshold, 1, treated)) %>%
                      fill(treated))
dat$treated[is.na(dat$treated)==T] <- 0

sub = dat[dat$v2x_polyarchy<=0.42,]
sub = sub[is.na(sub$v2smgovfilprc)==F & is.na(sub$treated)==F & is.na(sub$v2x_polyarchy)==F & is.na(sub$pres.election)==F & is.na(sub$coup.attempts)==F & is.na(sub$successful.coups)==F,]
sub = sub[is.na(sub$pcGDPcur)==F & is.na(sub$GDPcur)==F & is.na(sub$electricitypc)==F,]
sub = sub[is.na(sub$Obstacles.to.Access)==F & is.na(sub$Limits.on.Content)==F & is.na(sub$Violations.of.User.Rights)==F,]
nrow(dat);nrow(sub)

# all use min.T0=3
out1 <- gsynth(Obstacles.to.Access ~ treated + v2x_polyarchy + pres.election + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=3, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
out1$est.avg
out1est10m = out1$est.avg

out2 <- gsynth(Limits.on.Content ~ treated + v2x_polyarchy + pres.election + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=3, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
out2$est.avg
out2est10m = out2$est.avg

out3 <- gsynth(Violations.of.User.Rights ~ treated + v2x_polyarchy + pres.election + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=3, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
out3$est.avg
out3est10m = out3$est.avg

out4 <- gsynth(Total.country.score ~ treated + v2x_polyarchy + pres.election + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=3, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
out4$est.avg
out4est10m = out4$est.avg


# export table
ti = data.frame(rbind(out1est250,out1est500,out1est1m,out1est5m,out1est10m))
ti$term = c("Transfer \u2265 $250,000","Transfer \u2265 $500,000","Transfer \u2265 $1,000,000","Transfer \u2265 $5,000,000","Transfer \u2265 $10,000,000")
ti = ti[,c("term","Estimate","S.E.","p.value")]
names(ti) = c("term","estimate","std.error","p.value")
out1 = list(tidy=ti)
class(out1) = "modelsummary_list"

ti = data.frame(rbind(out2est250,out2est500,out2est1m,out2est5m,out2est10m))
ti$term = c("Transfer \u2265 $250,000","Transfer \u2265 $500,000","Transfer \u2265 $1,000,000","Transfer \u2265 $5,000,000","Transfer \u2265 $10,000,000")
ti = ti[,c("term","Estimate","S.E.","p.value")]
names(ti) = c("term","estimate","std.error","p.value")
out2 = list(tidy=ti)
class(out2) = "modelsummary_list"

ti = data.frame(rbind(out3est250,out3est500,out3est1m,out3est5m,out3est10m))
ti$term = c("Transfer \u2265 $250,000","Transfer \u2265 $500,000","Transfer \u2265 $1,000,000","Transfer \u2265 $5,000,000","Transfer \u2265 $10,000,000")
ti = ti[,c("term","Estimate","S.E.","p.value")]
names(ti) = c("term","estimate","std.error","p.value")
out3 = list(tidy=ti)
class(out3) = "modelsummary_list"

ti = data.frame(rbind(out4est250,out4est500,out4est1m,out4est5m,out4est10m))
ti$term = c("Transfer \u2265 $250,000","Transfer \u2265 $500,000","Transfer \u2265 $1,000,000","Transfer \u2265 $5,000,000","Transfer \u2265 $10,000,000")
ti = ti[,c("term","Estimate","S.E.","p.value")]
names(ti) = c("term","estimate","std.error","p.value")
out4 = list(tidy=ti)
class(out4) = "modelsummary_list"



myrows = data.frame(list(c("Country Fixed Effects",rep("Yes",4))
                         ,c("Year Fixed Effects",rep("Yes",4))
                         ,c("Control Variables",rep("Yes",4))
))

export = modelsummary(list("Internet Filtering" = out1
                           , "Internet Shutdowns" = out2
                           ,"Social Media Monitoring"=out3
                           ,"Arrests for Political Content"=out4
)
,add_rows=data.frame(t(myrows))
, stars=c('*' = .1, '**' = 0.05, '***' = .01)
, output="latex_tabular"
)
export = gsub("≥", "$\\\\ge$", export)
export

